<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 18/3/30
 * Time: 12:13
 */

namespace App\Services\Admin;


use Carbon\Carbon;

trait ChartTrait
{
    public function xAxis($type){
        $data = [];
        switch ($type){
            case 'day':
                for($i = 0;$i<=23;$i++){
                    $data[] = $i.':00';
                }
                break;
            case 'week':
                $data = ['周一','周二','周三','周四','周五','周六','周日'];
                break;
            case 'month':
                for($i = 1;$i<=date('t');$i++){
                    $data[] = $i;
                }
                break;
            case 'year':
                for($i = 1;$i<=12;$i++){
                    $data[] = $i.'月';
                }
                break;
        }
        return [
            'type'=>'category',
            'boundaryGap'=>false,
            'data'  =>  $data
        ];
    }
    public function getDateRange($type){
        $data = [];
        $start = Carbon::yesterday();
        $end = Carbon::today();
        switch ($type){
            case 'day':
                $start = Carbon::yesterday();
                $end = Carbon::today();
                break;
            case 'week':
                $start = Carbon::parse('-2 Monday')->toDateTimeString();
                $end = Carbon::parse('-1 Monday')->toDateTimeString();
                break;
            case 'month':
                $start = Carbon::create(date('Y',time()),date('m',time()),1,0,0,0);
                $end = Carbon::create(date('Y',time()),date('m',time()),1,0,0,0)->addMonth(1);
                break;
            case 'year':
                $start = Carbon::create(date('Y',time()),1,1,0,0,0);
                $end = Carbon::create(date('Y',time()),1,1,0,0,0)->addYear(1);
                break;
            default:
                $start = Carbon::yesterday();
                $end = Carbon::today();
                break;
        }
        $data = [
            'start' =>  $start,
            'end'   =>  $end
        ];
        return $data;
    }
    public function getGroupBy($type,$field='created_at'){
        $data = '';
        switch ($type){
            case 'day':
                $data = "%Y-%m-%d %H:00:00";
                break;
            case 'week':
                $data = "%Y-%m-%d 00:00:00";
                break;
            case 'month':
                $data = "%Y-%m-%d 00:00:00";
                break;
            case 'year':
                $data = "%Y-%m-01 00:00:00";
                break;
        }
        $str = "date_format(`{$field}`,'{$data}')";
        return $str;
    }
    public function parseData($data,$type='day'){
        $list = [];

        $start = Carbon::yesterday();
        $end = Carbon::today();
        switch ($type){
            case 'day':

                $start = Carbon::yesterday();
                $end = Carbon::today();
                $l = $end->diffInHours($start);
                for($i = 0;$i<$l;$i++){
                    $list[$start->addHour(1)->toDateTimeString()] = 0;
                }
                break;
            case 'week':
                $start = Carbon::parse('-2 Monday');
                $end = Carbon::parse('-1 Monday');
                $l = $end->diffInDays($start);
                for($i = 0;$i<$l;$i++){
                    $list[$start->addDay(1)->toDateTimeString()] = 0;
                }
                break;
            case 'month':
                $start = Carbon::create(date('Y',time()),date('m',time()),1,0,0,0);
                $end = Carbon::create(date('Y',time()),date('m',time()),1,0,0,0)->addMonth(1);
                $l = $end->diffInDays($start);
                for($i = 0;$i<$l;$i++){
                    $list[$start->addDay(1)->toDateTimeString()] = 0;
                }
                break;
            case 'year':
                $start = Carbon::create(date('Y',time()),1,1,0,0,0);
                $end = Carbon::create(date('Y',time()),1,1,0,0,0)->addYear(1);
                $l = $end->diffInMonths($start);
                for($i = 0;$i<$l;$i++){
                    $list[$start->addMonth(1)->toDateTimeString()] = 0;
                }
                break;
        }
        foreach ($data as $k=>$v){
            if(isset($list[$v->_time])){
                $list[$v->_time] = $v->_res;
            }
        }
        return array_values($list);
    }
}